<template>
    <div>
                <el-form  :model="form"  ref="ruleForm" label-width="80px" :rules="rules" >
                    <el-form-item label="手机号" prop="telephoneNumber">
                        <el-input v-model="form.telephoneNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input  type="password"  autocomplete="off" v-model="form.password"></el-input>
                    </el-form-item>

                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="form.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="form.email"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                        <el-radio-group v-model="form.sex">
                            <el-radio label="男">男</el-radio>
                            <el-radio label="女">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="生日" >
                        <el-date-picker
                                v-model="form.birth"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                        >
                        </el-date-picker>
                    </el-form-item>

                </el-form>
        <div class="btn">
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="success" @click="zhuce()">确 定</el-button>
        </div>

    </div>
</template>

<script>
    export default {
        name: "putongzhuce",
        data(){
            var checkmail = (rule, value, cb) => {
                var RegEmail =  /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/
                if(RegEmail.test(value)){return cb()}
                return cb(new Error('请输入合法的邮箱'))
            }
            var checkphone = (rule, value, cb) => {
                var RegEmail = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/

                if(RegEmail.test(value)){return cb()}
                return cb(new Error('请输入合法的手机号'))
            }
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPass !== '') {

                        this.$refs.form.validateField('checkPass');
                        // console.log(  this.$refs.form.validateField)
                        callback()
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };

            return{
                form:{
                    username:'',
                    sex:'男',
                    password:'',
                    role: '普通用户',
                    email:'',
                    birth:'',
                    telephoneNumber:''
                },
                rules:{
                    // username:[
                    //     { required: true, message: '请输入用户名', trigger: 'blur' },
                    //     { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 8, message: '密码长度为6-8', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { required: true, message: '确认密码', trigger: 'blur' },
                        { validator: validatePass2, trigger: 'blur' },
                    ],
                    email:[
                        { required: true, message: '请输入邮箱', trigger: 'blur' },
                        { validator:checkmail,trigger: 'blur' }],
                    telephoneNumber:[
                        { required: true, message: '请输入手机号', trigger: 'blur' },
                        { validator:checkphone,trigger: 'blur' }],
                }
            }
        },
        methods:{
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open(word) {
                this.$alert(word, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'info',
                            message: `action: ${ action }`
                        });
                    }
                });
            },

            zhuce(){
                this.$refs.ruleForm.validate(valid =>{
                    if(!valid)return
                    this.$http.post('user/register', this.form).then(response => {

                        console.log(response);
                        if (response.data.code === 200) {

                            this.$refs.ruleForm.resetFields();
                            this.open('注册成功！')
                        }
                        if (response.data.code !==200) {

                            this.open('注册失败！')
                        }
                    })
                })

            }
        }
    }
</script>

<style scoped>
    .btn{
        margin-left: 300px;
    }
</style>
